package com.ming.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ming.common.CodeMsg;
import com.ming.common.Constant;
import com.ming.common.Result;
import com.ming.domain.Book;
import com.ming.domain.BookReturn;
import com.ming.query.CommentQuery;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ming.domain.Comment;
import com.ming.mapper.CommentMapper;
import com.ming.service.CommentService;
import org.springframework.util.ObjectUtils;

/**
 *@Author:何元才
 *@Desc:
 *@data:2021-05-17 04:43
 */
@Service
public class CommentServiceImpl extends ServiceImpl<CommentMapper, Comment> implements CommentService{


    @Autowired
    private CommentMapper commentMapper;
    /**
     * 分页查询
     *
     * @param query
     * @return
     */
    @Override
    public Result queryPage(CommentQuery query) {
        //开启分页查询
        Page<Comment> page = new Page<>(query.getPage(),query.getLimit());
        Page<Comment> commentPage = commentMapper.selectPage(page, new LambdaQueryWrapper<Comment>()
                .eq(!ObjectUtils.isEmpty(query.getUserName()), Comment::getUserName,query.getUserName())
                .in(Comment::getDeleted,0)
        );
        return new Result(commentPage);
    }

    /**
     * 新增
     *
     * @param comment
     * @return
     */
    @Override
    public Result add(Comment comment) {
        comment.setDeleted(0);
        comment.setCreateTime(new Date());
        //新建一条评论信息
        Comment newcomment = new Comment();
        BeanUtils.copyProperties(comment,newcomment);
        //插入一条评论信息
        commentMapper.insert(newcomment);
        return new Result();
    }

    /**
     * 删除
     *
     * @param commentId
     * @return
     */
    @Override
    public Result del(Integer commentId) {
        //逻辑删除 修改就可以了  根据id找到当前选择的图书
        Comment comment = commentMapper.selectById(commentId);
        //无效
        if (comment == null || comment.getDeleted().equals(Constant.INVALID)) {
            return new Result(CodeMsg.USER_VALID_ERROR);
        }
        //设置失效
        comment.setDeleted(1);
        commentMapper.updateById(comment);
        return new Result();
    }
}
